package com.yhx.controller;

import com.yhx.iservice.ICheckitemService;
import com.yhx.pojo.Checkitem;
import com.yhx.utils.*;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

@RestController
@RequestMapping("/checkitem")
public class CheckitemController {
    @Reference
    private   ICheckitemService iCheckitemService;

    @Value("${upload.path}")
    private String path;
    @GetMapping("/list")
    public Result list(){
        try {
            List<Checkitem> list = iCheckitemService.list();
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
    @PostMapping("listpage")
    public PageResult pagelist(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pagelist = iCheckitemService.pagelist(queryPageBean);
            return pagelist;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    @PostMapping
    public Result save(@RequestBody Checkitem checkitem){
        try {
            int save = iCheckitemService.save(checkitem);
            if (save>0){
                return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
            }
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }

    }

    @GetMapping("/{id}")
    public Result getByid(@PathVariable int id){
        System.out.println(id);
        try {
            Checkitem checkitem = iCheckitemService.getByid(id);
            System.out.println(checkitem);
            if (checkitem!=null){
                return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkitem);
            }
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    @PutMapping
    public Result update(@RequestBody Checkitem checkitem){
        try {
            int update = iCheckitemService.update(checkitem);
            if(update>0){
                return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
            }
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable int id){
        try {
            int remove = iCheckitemService.delete(id);
            if(remove>0){
                return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
            }
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }

    @DeleteMapping
    public Result deleteAll(Integer[] ids){
        try {
            int i = iCheckitemService.deleteAll(ids);
            if (i>0){
                return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
            }
            return new Result(true, MessageConstant.DELETE_CHECKITEM_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("upload")
    public Result save(@RequestParam("excelFile")MultipartFile multipartFile){
        try {
            //上传文件
            File upload = MyFileUtils.upload(multipartFile, path);
            if(upload==null){
                return new Result(false, MessageConstant.UPLOAD_FAIL);
            }
            //上传成功
            //将文件读入一个list集合
            List<Checkitem> list = ExcelUtils.ExcelgetList2(upload);
            Result result = iCheckitemService.saveAll(list);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPLOAD_FAIL);
        }

    }
}
